//
//  TopViewController.m
//  卢卡的时光网
//
//  Created by 卡神 on 15/7/17.
//  Copyright © 2015年 lok. All rights reserved.
//

#import "TopViewController.h"
#import "common.h"
#import "DataService.h"
#import "TopModel.h"
#import "TopCell.h"

@interface TopViewController ()

@end

@implementation TopViewController

#pragma mark - lifecycle

- (nonnull instancetype)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.title = @"Top";
    }
    return self;
}

- (void)viewDidLoad {
    [self _loadData];
    [self _createCollectionView];
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - collectionView's function 

- (void)_createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.itemSize = CGSizeMake(kWidth / 3, 167);
    _collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 49) collectionViewLayout: layout];
    _collection.dataSource = self;
    _collection.delegate = self;
  //  [_collection setBackgroundColor: [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]]];
    [_collection registerNib:[UINib nibWithNibName:@"TopCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:topCell];
    [self.view addSubview:_collection];
}


- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [_topData count];
}



- (UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    TopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:topCell forIndexPath:indexPath];
    cell.model = _topData[indexPath.row];
    //[cell.contentView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]]];
    return cell;
}

#pragma mark - loadData 

- (void)_loadData {
    _topData = [[NSMutableArray alloc] init];
    NSDictionary *dic = [DataService readJsonFlieWithNamed:topJson];
    NSArray *array = [dic objectForKey:@"subjects"];
    for (NSDictionary *dc in array ) {
        TopModel *model = [[TopModel alloc] init];
        model.title = [dc objectForKey:@"title"];
        model.average = [[[dc objectForKey:@"rating"] valueForKey:@"average"] doubleValue];
        model.images = [dc valueForKey:@"images"];
        [_topData addObject:model];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
